package com.bingbaihanji.hibernate;


import com.bingbaihanji.hibernate.pojo.User;
import com.bingbaihanji.hibernate.util.HibernateUtil;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author 冰白寒祭
 * @date 2024-03-24 23:10:09
 * @description //TODO
 */
@Slf4j
public class TestHibernate {
    Transaction transaction = null;


    @Test
    public void relation(){
        try (
                SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
                Session session = sessionFactory.getCurrentSession()
        ) {
            //  开启事务
            transaction = session.beginTransaction();
            // 提交事务
            transaction.commit();
        } catch (Exception e) {
            log.error("系统错误操作异常参数为: ", e);
            // 事务回滚
            transaction.rollback();
        }
    }

    @Test
    public void sqlQueryTest() {
        try (
                SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
                Session session = sessionFactory.getCurrentSession()
        ) {
            //  开启事务
            transaction = session.beginTransaction();
//            NativeQuery sqlQuery = session.createSQLQuery(); // 弃用
            String sqlString = """
                    select
                        *
                    from
                        user
                    where
                        user_name = '张长永'
                    """;
            // 返回结果是数组
            NativeQuery nativeQuery = session.createNativeQuery(sqlString);
            List<Object[]> list = nativeQuery.list();
            for (Object[] objects : list) {
                log.info("userList结果: {}", Arrays.stream(objects).toList());
            }

//            for (int i = 0; i < list.size(); i++) {
//                for (int j = 0; j < list.get(i).length; j++) {
//                    System.out.println(list.get(i)[j]);
//                }
//            }

//            for (Object[] objects : list) {
//                System.out.println(Arrays.toString(objects));
//            }
            // 将结果转化为对象
            nativeQuery.addEntity(User.class);
            List<User> list1 = nativeQuery.list();
            for (User user : list1) {
                log.info("转化后的结果: {}", user);
            }


            // 提交事务
            transaction.commit();
        } catch (Exception e) {
            log.error("系统错误操作异常参数为: ", e);
            // 事务回滚
            transaction.rollback();
        }
    }

    @Test
    public void criteriaTest() {
        try (
                SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
                Session session = sessionFactory.getCurrentSession()
        ) {
            //  开启事务
            transaction = session.beginTransaction();
            Criteria criteria = session.createCriteria(User.class); // 已弃用
            List<User> list = criteria.list();
            for (User user : list) {
                log.info("返回结果user为: {}", user);
            }
            // 提交事务
            transaction.commit();
        } catch (Exception e) {
            log.error("系统错误操作异常参数为: ", e);
            // 事务回滚
            transaction.rollback();
        }
    }

    @Test
    public void queryTest() {
        try (
                SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
                Session session = sessionFactory.getCurrentSession()
        ) {
            //  开启事务
            transaction = session.beginTransaction();

            // 创建Query 对象
            Query fromUser = session.createQuery("from User");

            // 获取返回结果
            List<User> resultList = fromUser.list();
            for (User user : resultList) {
                log.info("返回结果user为: {}", user);
            }

            // 提交事务
            transaction.commit();
        } catch (Exception e) {
            log.error("系统错误操作异常参数为: ", e);
            // 事务回滚
            transaction.rollback();
        }
    }
}
